<?php
class Admin_member extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('users/users_model','users');
		$this->load->model('usertype/usertype_model','usertype');
		$this->load->model('order/order_list_model','order_list');
		$this->load->model('order/order_detail_model','order_detail');
		$this->load->model('product/product_model','product');
		$this->load->model('product/product_category_model','pcategory');		
		$this->load->model('product/product_compatible_model','product_compat');
		$this->load->model('admin_coupon/coupon_model','coupon');
	}
	
	public function index()
	{
		//$this->db->debug = true;
		$data['url'] = GetCurrentUrlGetParameter();
		$menu_id=7;
		$data['menu_id'] = $menu_id;
		$data['urlpage']="admin_member";
		if(is_login()){
			if(permission($menu_id, 'canview')=='')redirect('admin');
			$condition = @$_GET['search'] != '' ?  " isadmin=0  AND (email LIKE '%".$_GET['search']."%' OR users.name LIKE '%".$_GET['search']."%') " : " isadmin=0 ";
			//$condition .= @$_GET['type']!='' ? " AND page='".$_GET['type']."'" : "";			
			$data['result'] = $this->users->where($condition)->get();
			$data['pagination'] = $this->users->pagination();					
			$this->template->build('index',$data);
		}
		else{
			
			redirect('admin');	
		}
	}	
	function form($id=false)
	{
		$data['url'] = GetCurrentUrlGetParameter();
		$menu_id=7;		
		$menu_name = GetMenuProperty($menu_id,'title');
		$data['id']=$id;
		$data['lang']='th';
		if(is_login())
		{
			if(permission($menu_id, 'canview')=='')redirect('admin');			
			$data['rs'] = @$this->users->where($condition)->get_row($id);
			$data['usertype'] = @$this->usertype->get(FALSE,TRUE);
			$data['province'] = $this->db->getarray("SELECT * from province ORDER BY name_".$data['lang']);
			$data['country'] = $this->db->getarray("SELECT * from country ORDER BY name");					
			if($id>0){
				$data['lang'] = GetCurrLang();
				$data['urlpage'] = 'admin_order';
				$condition = @$_GET['search']!='' ? " member_id=".$id." AND (order_no LIKE '%".$_GET['search']."%' OR shipping_name LIKE '%".$_GET['search']."%' OR billing_name LIKE '%".$_GET['search']."%') " : " member_id=".$id;
				$data['result'] = $this->order_list->where($condition)->order_by('id','desc')->get();
				$data['pagination'] = $this->order_list->pagination();			
				
				$action='View';
				$description = $action.' '.$menu_name.' : '.$data['rs']['name'];		
				save_log($menu_id,$action,$description);		
			}
			$this->template->build('form',$data);
		}
		else{
			
			redirect('admin');	
		}
	}
	public function save(){
		//$this->db->debug = true;
		$url = GetCurrentUrlGetParameter();
		$menu_id=7;
		$menu_name = GetMenuProperty($menu_id,'title');
		if($_POST['id']!='')
		{
			if(permission($menu_id, 'canedit')=='')redirect('admin_member');
			$action='Update';
			$description = $action.' '.$menu_name.' : '.$_POST['name'];		
			save_log($menu_id,$action,$description);
		}else{
			if(permission($menu_id, 'canadd')=='')redirect('admin_member');	
			$action='Add';
			$description = $action.' '.$menu_name.' : '.$_POST['name'];		
			save_log($menu_id,$action,$description);
		}
		$_POST['password'] =  $_POST['id']!='' && $_POST['password']=='' ? $_POST['current_password'] : md5($_POST['password']);
		$_POST['registerdate'] =  $_POST['id']!='' && $_POST['password']=='' ? $_POST['registerdate'] : date("Y-m-d H:i:s");
		$id = $this->users->save($_POST);		
		//set_notify('success', lang('save_data_complete'));
		redirect('admin_member'.$url);
	} 
	function delete($id=FALSE){
		$menu_id=7;
		$menu_name = GetMenuProperty($menu_id,'title');
		if(permission($menu_id, 'candelete')=='')redirect('admin_member');
		$users = $this->users->get_row($id);
		$this->users->delete($id);
		$action='Delete';
		$description = $action.' '.$menu_name.' : '.$users['name'];		
		save_log($menu_id,$action,$description);
		redirect('admin_member');
	}
	
	public function export()
	{
		$filename= "export_member_".date("Y-m-d_H_i_s").".xls";
		header("Content-Disposition: attachment; filename=".$filename);  
		$menu_id=7;
		$menu_name = GetMenuProperty($menu_id,'title');
		$data['menu_id'] = $menu_id;		
		$data['urlpage']="admin_member";
			$condition = @$_GET['search'] != '' ?  " isadmin=0  AND (email LIKE '%".$_GET['search']."%' OR name LIKE '%".$_GET['search']."%') " : " isadmin=0 ";
			//$condition .= @$_GET['type']!='' ? " AND page='".$_GET['type']."'" : "";			
			$data['result'] = $this->users->where($condition)->get(FALSE,TRUE);
			$action='Export';
			$description = $action.' '.$menu_name;		
			save_log($menu_id,$action,$description);
			$this->load->view('export',$data);
			//$this->template->build('export',$data);
	}	
}
?>